package com.example.asus.spinnerdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SpinnerDemo2 extends AppCompatActivity {
    private static final String[] countriesStr ={ "广州", "从化", "武汉", "汕头" };
    private TextView myTextView;
    private EditText myEditText;
    private Button myButton_add;
    private Button myButton_remove;
    private Spinner mySpinner;
    private ArrayAdapter adapter;
    private List allCountries;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_demo2);
        allCountries=new ArrayList();
        for(int i=0;i<countriesStr.length;i++)
        {
            allCountries.add(countriesStr[i]);
        }
        myTextView=(TextView) findViewById(R.id.myTextView);
        myEditText=(EditText) findViewById(R.id.myEditText);
        myButton_add=(Button) findViewById(R.id.myButton_add);
        myButton_remove=(Button) findViewById(R.id.myButton_remove);
        mySpinner=(Spinner) findViewById(R.id.mySpinner);
        adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,allCountries);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        //将ArrayAdapter加入Spinner对象中
        mySpinner.setAdapter(adapter);
        //将myButton_add加入OnClickListener
        myButton_add.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                String newCountry=myEditText.getText().toString();
                for(int i=0;i<adapter.getCount();i++)//检查新增值是否已存在，不存在则添加
                {
                    if(newCountry.equals(adapter.getItem(i)))
                    {
                        return;
                    }
                }
                if(!newCountry.equals(""))//新增值不为空
                {
                    adapter.add(newCountry);
                    int position=adapter.getPosition(newCountry);
                    mySpinner.setSelection(position);//将Spinner选取在新增值的位置
                    myEditText.setText("");//将myEditText清空
                }
            }
        });

        myButton_remove.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                if(mySpinner.getSelectedItem()!=null)
                {
                    adapter.remove(mySpinner.getSelectedItem().toString());
                    myEditText.setText("");
                    if(adapter.getCount()==0)
                    {
                        myTextView.setText("");
                    }
                }
            }
        });
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                myTextView.setText(parent.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
